package styledaco.board;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import statics.SoptVars;
import styledaco.member.MemberProc;

import com.ibatis.sqlmap.client.SqlMapClient;

import comm.util.CommUtil;
import comm.util.RequestManager;
import comm.util.SoptHashMap;
import comm.util.SqlMapLoader;

public class CategoryProc {
	private SqlMapClient sqlMapper = SqlMapLoader.getInstance();
	private Logger logger = Logger.getLogger(MemberProc.class);
	
	public String regCategory(HttpServletRequest request) throws UnsupportedEncodingException{
		String result = SoptVars.FAIL;
		SoptHashMap hm = RequestManager.getRequest(request);
		try {
			result = CommUtil.NVL(sqlMapper.insert("category.regCategory", hm),"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return result;
		}
		
		return result;
		
	}
	
	public ArrayList getCategoryList(String type){
		ArrayList lt = null;
		
		try{
			if(type.length()>0){
				lt = (ArrayList) sqlMapper.queryForList("category.getCategoryListByType",type);
			}else{
				lt = (ArrayList) sqlMapper.queryForList("category.getCategoryList");
			}
		}catch (Exception e) {
			return lt;
		}
		
		return lt;
	}
	
	public String getCategorySeqOfFirstInCurrentType(String type){
		ArrayList lt = null;
		String curCategory = "";
		try{
			if(type.length()>0){
				lt = (ArrayList) sqlMapper.queryForList("category.getCategoryListByType",type);
			}
		}catch (Exception e) {
			lt = null;
		}
		if(lt!=null && lt.size()>0)
			curCategory = ((SoptHashMap)lt.get(0)).get("CATEGORY_SEQ").toString();
		return curCategory;
	}
	
	public SoptHashMap getCategoryBySeq(String seq){
		SoptHashMap hm = null;
		try {
			hm = (SoptHashMap)sqlMapper.queryForObject("category.getCategoryBySeq",seq);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hm;
	}
	
	
}
